layui.use(['layer', 'form', 'table', 'laydate','admin', 'ax'], function () {
    //var $ = layui.$;
    var $= layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    /**
     * 系统管理--评价管理
     */
    var OBJ = {
        tableId: "rechargeTable",    //表格id
        condition: {
            rechargeOrderNo: "",
            shopName:""
        }
    };

    /**
     * 初始化表格的列
     */
    OBJ.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'rechargeOrderNo', sort: true, title: '订单号',width:200},
            {field: 'shopName', sort: true, title: '商户名称'},
            {field: 'shopAdr', sort: true, title: '商户地址',width:200},
            {field: 'rechargeAmt', sort: true, title: '收费金额'},
            {field: 'statusName', sort: true, title: '状态'},
            {field: 'createTime', sort: true, title: '创建时间',width:200},
            {align: 'left', toolbar: '#tableBar', title: '操作', minWidth: 200,fixed: 'right'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    OBJ.search = function () {
        var queryData = {};
        queryData['rechargeOrderNo'] = $("#rechargeOrderNo").val().replace(/\s+/g,"");
        queryData['shopName'] = $("#shopName").val().replace(/\s+/g,"");
        queryData['shopAdr'] = $("#shopAdr").val().replace(/\s+/g,"");
        queryData['createTime'] = $("#createTime").val();
        queryData['rechargeStatus'] = $("#rechargeStatus").val();
        table.reload(OBJ.tableId,{
            page:{
                curr:1
            },
            where: queryData
        })
    };
    //渲染时间选择框
    laydate.render({
        elem: '#createTime',
        range: true,
        max: Feng.currentDate()
    });
    /**
     * 弹出添加角色
     */
    OBJ.openAddRecharge = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '新增收费',
            content: Feng.ctxPath + '/recharge/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(OBJ.tableId);
            }
        });
    };
    /**
     * 点击详情
     *
     * @param data 点击按钮时候的行数据
     */
    OBJ.onDetailRecharge = function (data) {
        admin.putTempData('formOk', false);
        var statusName = encodeURI(data.statusName);
        top.layui.admin.open({
            type: 2,
            area: '1000px',
            title: '充值详情',
            content: Feng.ctxPath + '/recharge/recharge_detail?id=' + data.id+'&statusName='+statusName,
            end: function () {
                admin.getTempData('formOk') && table.reload(OBJ.tableId);
            },
            btn: ['返回'],
            btnAlign: 'c'
        });
    };


    /**
     * 导出excel按钮
     */
    OBJ.exportExcel = function () {
        var checkRows = table.checkStatus(OBJ.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OBJ.tableId,
        url: Feng.ctxPath + '/recharge/list',
        page: {
            limits:[10,20,30,50,100,'ALL']
        },
        height: "full-158",
        title:"充值管理",
        cellMinWidth: 100,
        cols: OBJ.initColumn(),
        done:function(res, curr, count){
            if (this.limit ==count){
                $(".layui-laypage-limits select").find("option[value='ALL']").attr("selected",true);
            }
            $(".layui-laypage-limits select").find("option[value='ALL']").attr("value",count).text('全部');
        }
    });
    /**
     * 点击录卡
     *
     * @param data 点击按钮时候的行数据
     */
    OBJ.onSaveCard = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '录入电卡资料',
            content: Feng.ctxPath + '/opencard/save_card?opencardId=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(OBJ.tableId);
            }
        });
    };
    /**
     * 点击派单
     *
     * @param data 点击按钮时候的行数据
     */
    OBJ.onDispatch = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '派单',
            content: Feng.ctxPath + '/opencard/opencard_dispatch?opencardId=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(OBJ.tableId);
            }
        });
    };
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OBJ.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        OBJ.openAddRecharge();
    });
    // 导出excel
    $('#btnExp').click(function () {
        OBJ.exportExcel();
    });


    // 工具条点击事件
    table.on('tool(' + OBJ.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'card') {
            OBJ.onSaveCard(data);
        } else if (layEvent === 'dispatch') {
            OBJ.onDispatch(data);
        } else if (layEvent === 'detail') {
            OBJ.onDetailRecharge(data);
        }
    });
});
